<?php
/**
 * Created by PhpStorm.
 * User: sunzhaoquan
 * Date: 2017/4/13
 * Time: 23:04
 */
namespace Admin\Controller;
use Think\Controller;
use Admin\Common\AdminController;
class BlogController extends AdminController {
    function showlist(){
        //查询数据库中数据的条数
        $rows = D('Blog')->count();
        //定义每页显示的条数
        $per = 3;
        //引入分页类
        $page=new \Think\Page($rows,$per);
        //配置
        $page->setConfig('header',"共 %TOTAL_ROW% 条记录");
        $page->setConfig('prev',"上一页");
        $page->setConfig('next',"下一页");
        $page->setConfig('last',"%TOTAL_PAGE%");
        $page->setConfig('theme',"%FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END%");
        //调用show方法显示分页内容
        $pagelist = $page->show();
        //博文信息展示
        $blog_info = D('Blog as b')
            ->join('bg_cate as c on b.cate_id=c.cate_id')
            ->limit($page->firstRow.','.$page->listRows)
            ->select();
        $this->assign('blog_info',$blog_info);
        $this->assign('pagelist',$pagelist);
        $this->display();
    }
    function add(){
        $blog = D('Blog');
        if(IS_POST){
            //表单的收集
            $data = I('post.');
            //富文本，使用fangXSS进行过滤
            $data['blog_content']= \fangXSS($_POST['blog_content']);
            //添加字段内容
            $data['add_time']=$data['upd_time']=time();
            if($id = $blog->add($data)){
                $this->success('添加成功',U('Blog/showlist'),2);
            }else{
                $this->error('添加失败',U('Blog/add'),3);
            }
        }else{
            //分类列表展示
            $cate_info = D('Cate')->select();
            $this->assign('cate_info',$cate_info);
        }
        $this->display();
    }
    function upd(){
        if(IS_POST){
            $data = I('post.');
            //富文本，使用fangXSS进行过滤
            $data['blog_content']= \fangXSS($_POST['blog_content']);
            if(D('Blog')->save($data)){
                $this->success('修改成功',U('Blog/showlist'),2);
            }else{
                $this->success('修改',U('Blog/upd',array('blog_id'=>$data['blog_id'])),3);
            }
        }else{
            //表单的展示
            $blog_id = I('get.blog_id');
            //获取表单信息
            $blog_info=D('Blog')
                ->where(array('blog_id'=>$blog_id))
                ->select();
            $cate_info = D('Cate')->select();
            $this->assign('cate_info',$cate_info);
            $this->assign('blog_info',$blog_info);
            $this->display();
        }
    }
    function del(){
        $blog_id = $_POST['blog_id'];
        $logo_info = D('Blog')->where(array('blog_id'=>$blog_id))->find();
        if(file_exists($logo_info['blog_big_logo'])){unlink($logo_info['blog_big_logo']);}
        if(file_exists($logo_info['blog_small_logo'])){unlink($logo_info['blog_small_logo']);}
        if(D('Blog')->delete($blog_id)){
            echo json_encode(array('status'=>1));
        }else{
            echo json_encode(array('status'=>0));
        }

    }
}